﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Xml.Serialization;
using ElectronicCommerce.Framework.Collections;

namespace ElectronicCommerce.Framework.Diagnostics
{
    /// <summary>
    /// 扩展属性集合。
    /// </summary>
    [Serializable]
    [XmlRoot("ExtendedProperties")]
    public class ExtendedPropertyCollection : KeyedCollection<ExtendedProperty>
    {

        /// <summary>
        /// Extended Property Collection
        /// </summary>
        public ExtendedPropertyCollection()
        {
        }

        /// <summary>
        /// Extended Property Collection
        /// </summary>
        /// <param name="values">Name Value Collection</param>
        public ExtendedPropertyCollection(NameValueCollection values)
        {
            if (values != null)
            {
                foreach (string key in values)
                {
                    this.Add(key, values[key]);
                }
            }
        }

        /// <summary>
        /// Extended Property Collection
        /// </summary>
        /// <param name="keyValues">Extended PropertyCollection</param>
        public ExtendedPropertyCollection(IEnumerable<KeyValuePair<string, object>> keyValues)
        {
            if (keyValues != null)
            {
                foreach (KeyValuePair<string, object> keyValue in keyValues)
                {
                    this.Add(keyValue.Key, Convert.ToString(keyValue.Value));
                }
            }
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="name">Name String</param>
        /// <param name="values">Name Value Collection</param>
        public void Add(string name, NameValueCollection values)
        {
            ExtendedProperty property = new ExtendedProperty();
            property.Name = name;
            property.SubExtendedProperties = new ExtendedPropertyCollection(values);

            this.Add(property);
        }

        /// <summary>
        /// Add Name
        /// </summary>
        /// <param name="name">Name String</param>
        /// <param name="keyValues">KetValue Pair</param>
        public void Add(string name, IEnumerable<KeyValuePair<string, object>> keyValues)
        {
            ExtendedProperty property = new ExtendedProperty();
            property.Name = name;
            property.SubExtendedProperties = new ExtendedPropertyCollection(keyValues);

            this.Add(property);
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="name">Name</param>
        /// <param name="value">Value</param>
        public void Add(string name, string value)
        {
            this.Add(new ExtendedProperty(name, value));
        }
    }
}
